home *** CD-ROM | disk | FTP | other *** search
- #
- # checkIP.py
- # JunkMatcher
- #
- # Created by Benjamin Han on 2/1/05.
- # Copyright (c) 2005 Benjamin Han. All rights reserved.
- #
-
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
-
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
-
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
- #!/usr/bin/env python
-
- import socket, threading
-
-
- class GetHostByNameThread (threading.Thread):
- def __init__ (self, ipr):
- threading.Thread.__init__(self,target = self._myRun)
- self.result = None
- self.ipr = ipr
- def _myRun (self):
- try: self.result = socket.gethostbyname(self.ipr)
- except: pass
-
-
- def checkIP (ip, host, timeout):
- """Queries a single blacklist host about a single IP, using the specified
- timeout value; returns True iff the IP is an open relay."""
- ipl = ip.split('.')
- ipl.reverse()
- ipr = "%s.%s" % ('.'.join(ipl), host)
-
- # start a thread for gethostbyname(), and quits waiting for it if time is up
- getHostByNameThread = GetHostByNameThread(ipr)
- getHostByNameThread.start()
- if timeout: getHostByNameThread.join(timeout)
- else: getHostByNameThread.join()
-
- if getHostByNameThread.result and getHostByNameThread.result.startswith('127.'):
- return True
-
- return False
-
- def checkIPList (ipList, timeout, hostList):
- """Checks a list IPs using the blacklist 'hostList'; returns None if no open relay
- is found; otherwise returns a tuple (ip, host)."""
- for ip in ipList:
- for host in hostList:
- if checkIP(ip, host, timeout):
- return (ip, host)
-
- return None
-
-
- if __name__ == "__main__":
- import time
-
- HOST = 'bl.spamcop.net'
-
- startTime = time.time()
- print "66.103.45.5:",checkIP("66.103.45.5", host = HOST, timeout=0.5)
- endTime = time.time()
- print endTime - startTime
-
- print "128.2.203.179:",checkIP("128.2.203.179", host = HOST, timeout = 0.5)
- endTime = time.time()
- print endTime - startTime
-